﻿using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;

public class MosterController : MonoBehaviour
{
    public float speed;
    public float changeTimer = 3;

    private int direction = -1;
    private float timer;
    private Rigidbody2D rigidbody2d;
    private Animator animator;

    private float monsterAttack = 1;
    private AIPath aiPath;

    // Start is called before the first frame update
    void Start()
    {
        //获取敌人刚体
        rigidbody2d = GetComponent<Rigidbody2D>();
        timer = changeTimer;
        animator = GetComponent<Animator>();
        animator.SetFloat("MoveX", direction);
        animator.SetFloat("dead", direction);
        aiPath = GetComponent<AIPath>();

    }

    // Update is called once per frame
    void Update()
    {
        aiPath.canMove = GameStateManager.GameState == GameState.Playing;
        animator.speed = aiPath.canMove ? 1 : 0;
    }

    //设置怪物碰撞体
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //获取圆圈的碰撞体
        TowerController towerController = collision.transform.GetComponent<TowerController>();
        //获取人物碰撞体
        PlayerController playerController = collision.transform.GetComponent<PlayerController>();
        //碰到圆圈时改变动画为攻击
        if (towerController != null)
        {
            towerController.towerBeAttacked(monsterAttack);
            animator.SetFloat("MoveX", 1);
        }
        if (playerController != null)
        {
            animator.SetFloat("dead", 1);
            playerController.killMonster();
            Destroy(this.gameObject);
        }

    }


}
